# -*- coding: utf-8 -*-
from django.http import HttpResponse

from geraldo.generators import PDFGenerator
from geraldo import Report, ReportBand, ObjectValue, ReportBand, \
        landscape, SystemField, BAND_WIDTH, Label
        
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A4
from reportlab.lib.enums import TA_CENTER, TA_RIGHT

from intercampi.models.aluno import Aluno

def alunosPrimeiroUltimoEvento(request):
    
    relatorio = HttpResponse(mimetype='application/pdf')
    relatorio['Content-Disposition'] = 'filename=Relatorio de Alunos e Eventos.pdf'

    alunos = Aluno.objects.order_by('id')
    rel = RelatorioAlunosPrimeiroUltimoEvento(queryset=alunos)
    rel.generate_by(PDFGenerator, filename=relatorio)

    return relatorio


class RelatorioAlunosPrimeiroUltimoEvento(Report):
    
    title = 'Relatorio de Alunos e Eventos'
    print_if_empty = True
    page_size = landscape(A4)

    class band_detail(ReportBand):
        height = 1.5*cm
        elements=(
                ObjectValue(attribute_name='id', left=0.5*cm),
                ObjectValue(attribute_name='pessoa.nome', left=3*cm),
                ObjectValue(attribute_name='pessoa.telefone1', left=7*cm),
                ObjectValue(attribute_name='pessoa.telefone3', left=12*cm),
                ObjectValue(attribute_name='turma', get_value=lambda instance: \
                            (instance.turmas.all().order_by('-dataInicio')[0]), left=17*cm),
                ObjectValue(attribute_name='turma', get_value=lambda instance: \
                            (instance.turmas.all().order_by('dataInicio')[0]), left=22*cm)
                )
        
    class band_page_header(ReportBand):
        height = 1.3*cm
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
                Label(text="ID", top=0.8*cm, left=0.5*cm),
                Label(text=u"Nome", top=0.8*cm, left=3*cm),
                Label(text=u"Telefone Residencial", top=0.8*cm, left=7*cm),
                Label(text=u"Celular", top=0.8*cm, left=12*cm),
                Label(text=u"Ultimo Evento", top=0.8*cm, left=17*cm),
                Label(text=u"Primeiro Evento", top=0.8*cm, left=22*cm),
                SystemField(expression=u'Pagina %(page_number)d de %(page_count)d', top=0.1*cm,
                    width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom': True}

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                Label(text='Intercampi', top=0.1*cm),
                SystemField(expression=u'Impresso em %(now:%d/%m/%Y)s as %(now:%H:%M)s', top=0.1*cm,
                    width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'top': True}